//
//  SKFoodTopView.m
//  Gastrosoph
//
//  Created by 张明磊 on 12-10-31.
//
//

#import "SKFoodTopView.h"
#import "SKFoodTopCell.h"
#import "SKModel_foodTopRank.h"
#import "SKModel_tourist.h"


#define SK_TABLEVIEW_FOODTOP_RECT CGRectMake(0, 0, 320, 409)

@implementation SKFoodTopView

@synthesize tableView_rankTable = _tableView_rankTable;

@synthesize cell_foodTopCell = _cell_foodTopCell;

@synthesize array_foodTopModel = _array_foodTopModel;



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        self.tableView_rankTable = [[[UITableView alloc] initWithFrame: SK_TABLEVIEW_FOODTOP_RECT] autorelease];
        
        [_tableView_rankTable setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"view_shake_background"]]];
        
        _tableView_rankTable.dataSource = self;
        
        _tableView_rankTable.delegate = self;
        
        [self install_model_foodTop];
        
        [self addSubview:_tableView_rankTable];
        
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


- (void)dealloc
{
    [_tableView_rankTable release], _tableView_rankTable = nil;
    
    [_cell_foodTopCell release], _cell_foodTopCell = nil;
    
    [_array_foodTopModel release], _array_foodTopModel = nil;
    
    [super dealloc];
}


#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_array_foodTopModel count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        _cell_foodTopCell = nil;
        
        SKModel_foodTopRank *tmp_model = [self.array_foodTopModel objectAtIndex:indexPath.row];
        
        //_cell_foodTopCell = [[[SKFoodTopCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier withModel:tmp_model]autorelease];
        
        self.cell_foodTopCell = [[[SKFoodTopCell alloc] initwithModel:tmp_model] autorelease];
        
        cell = _cell_foodTopCell;
    }
    
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    [cell.contentView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"view_shake_background"]]];
    
    return cell;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


#pragma mark - 
#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SKModel_foodTopRank *tmpModel = [self.array_foodTopModel objectAtIndex:indexPath.row];
    if(tmpModel.integer_commentCount > 3)
    {
        return 500;
    }
    else
    {
        switch (tmpModel.integer_commentCount)
        {
            case 0:
            {
                return 400;
                break;
            }
                
            case 1:
            {
                return 420;
                break;
            }
                
            case 2:
            {
                return 440;
                break;
            }
                
            case 3:
            {
                return 460;
                break;
            }
                
            default:
                break;
        }
    }
    
    return 500;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


#pragma mark - 
#pragma mark Install_model_foodTop

- (void)install_model_foodTop
{
   NSArray * array_tourist = [self install_array_tourist];
    
   NSArray * array_tourist1 = [self install_array_tourist_test];
    
   SKModel_foodTopRank *model = [[SKModel_foodTopRank alloc] initWithUesrHeadPortrait:[UIImage imageNamed:@"test_foodTopCell_headPortrait.png"]
         foodName:@"奶油烘马铃薯"
    shareUserName:@"xyz_love"
        shareTime:@"14小时前"
        foodImage:[UIImage imageNamed:@"test_foodImage.png"]restaurantName:@"在 赛迪大厦餐厅"
         distance:@"1.13km"
     wantEatCount:7
     commentCount:4
     touristArray:array_tourist
         foodRate:0];
    
    SKModel_foodTopRank *model1 = [[SKModel_foodTopRank alloc] initWithUesrHeadPortrait:[UIImage imageNamed:@"test_user_4.png"]
          foodName:@"奶香三明治"
     shareUserName:@"zmlmagic"
         shareTime:@"8小时前"
         foodImage:[UIImage imageNamed:@"test_foodImage2.png"]restaurantName:@"在 汉拿山烧烤店"
          distance:@"2.7km"
      wantEatCount:3
      commentCount:2
      touristArray:array_tourist1
          foodRate:1];
    
    SKModel_foodTopRank *model2 = [[SKModel_foodTopRank alloc] initWithUesrHeadPortrait:[UIImage imageNamed:@"test_user_1.png"]
          foodName:@"油煎面包片"
     shareUserName:@"测试员"
         shareTime:@"2小时前"
         foodImage:[UIImage imageNamed:@"test_foodImage2.png"]restaurantName:@"在 金钱豹"
          distance:@"12.7km"
      wantEatCount:2
      commentCount:0
      touristArray:nil                                              foodRate:2];
    
    
    if(!_array_foodTopModel)
    {
        self.array_foodTopModel = [NSMutableArray arrayWithObjects:model,model1,model2,nil];
    }
    
    else
    {
        [self.array_foodTopModel addObject:model];
        
        [self.array_foodTopModel addObject:model1];
        
        [self.array_foodTopModel addObject:model2];
    }
    
    [model release];
    
    [model1 release];
    
    [model2 release];
}

- (NSArray *)install_array_tourist
{
    
    SKModel_tourist * tourist_1 = [[SKModel_tourist alloc] initWithTouristName:@"xyz_love:" andTouristComment:@"味道不错,再次光临啦~"];
    
    SKModel_tourist * tourist_2 = [[SKModel_tourist alloc] initWithTouristName:@"zmlmagic_tzxmagic:" andTouristComment:@"味道不错,再次光临啦~"];
    
    SKModel_tourist * tourist_3 = [[SKModel_tourist alloc] initWithTouristName:@"梧桐的天空:" andTouristComment:@"好有食欲啊,就是不知道人多不多,要不要摇号啊,哈哈,回头我也去尝尝."];
    
    SKModel_tourist * tourist_4 = [[SKModel_tourist alloc] initWithTouristName:@"天空中下起了淡淡的雨:"andTouristComment:@"测试长度测试长度12321421sdwfewfewfe"];
    
    NSArray *array_tmp = [NSArray arrayWithObjects:tourist_1,tourist_2,tourist_3,tourist_4,nil];
    
    [tourist_1 release];
    
    [tourist_2 release];
    
    [tourist_3 release];
    
    [tourist_4 release];
    
    return array_tmp;
}

- (NSArray *)install_array_tourist_test
{
    SKModel_tourist * tourist_1 = [[SKModel_tourist alloc] initWithTouristName:@"zmlmagic:" andTouristComment:@"测试"];
    
    SKModel_tourist * tourist_2 = [[SKModel_tourist alloc] initWithTouristName:@"测试人员:" andTouristComment:@"1234567890abcdefghijklmnopqrstuvwxyz"];
    
    NSArray *array_tmp = [NSArray arrayWithObjects:tourist_1,tourist_2,nil];
    
    [tourist_1 release];
    
    [tourist_2 release];
    
    return array_tmp;
}




@end
